﻿using Microsoft.Data.Sqlite;
using Microsoft.Win32;
using SQLitePCL;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace objass
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private EditNodeWindow nodeControl;
        private EditRelWindow edit2Control;
        private SearchWindow searchControl;

        public MainWindow()
        {
            InitializeComponent();
            Batteries.Init(); // 初始化 SQLitePCL 库
            nodeControl = new EditNodeWindow(); // 假设 EditWindow 是编辑的 UserControl
            edit2Control = new EditRelWindow(); // 假设 Edit2Window 是新的编辑的 UserControl
            searchControl = new SearchWindow(); // 假设 SearchWindow 是搜索的 UserControl

            // 注册 MainWindow 的尺寸变化事件
            this.SizeChanged += MainWindow_SizeChanged;
        }

        private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // 调整 MyWindowContainer 中的控件尺寸
            if (MyWindowContainer.Content == searchControl)
            {
                searchControl.Width = MyWindowContainer.ActualWidth;
                searchControl.Height = MyWindowContainer.ActualHeight;
            }
            else if (MyWindowContainer.Content == nodeControl)
            {
                nodeControl.Width = MyWindowContainer.ActualWidth;
                nodeControl.Height = MyWindowContainer.ActualHeight;
            }
            else if (MyWindowContainer.Content == edit2Control)
            {
                edit2Control.Width = MyWindowContainer.ActualWidth;
                edit2Control.Height = MyWindowContainer.ActualHeight;
            }
        }

        private void ButtonNew_Click(object sender, RoutedEventArgs e)
        {
            // 打开新建文件对话框
            SaveFileDialog saveFileDialog = new()
            {
                Filter = "SQLite Database|*.db",
                Title = "新建 SQLite 数据库文件"
            };

            if (saveFileDialog.ShowDialog() == true)
            {
                // 获取文件路径
                string filePath = saveFileDialog.FileName;

                // 创建 SQLite 数据库文件
                // 调用 App 类的 CreateSQLiteDatabase 方法
                var app = (App)Application.Current;
                app.CreateSQLiteDatabase(filePath);

                // 将文件路径推送给 DataGridSysBaseInfo 显示
                var data = new List<KeyValueInfo>
                            {
                                new KeyValueInfo { Key = "文件路径", Value = filePath },
                                new KeyValueInfo { Key = "文档信息", Value = "" }
                            };
                DataGridSysBaseInfo.ItemsSource = data;
                nodeControl.LoadNodes();
            }
        }

        private void ButtonOpen_Click(object sender, RoutedEventArgs e)
        {
            // 打开现有文件对话框
            OpenFileDialog openFileDialog = new()
            {
                Filter = "SQLite Database|*.db",
                Title = "打开 SQLite 数据库文件"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                string filePath = openFileDialog.FileName;

                // 使用类名调用静态方法
                if (!App.VerifyDatabase(filePath, null))
                {
                    // 弹出错误对话框
                    MessageBox.Show("数据库验证失败，请检查数据库文件。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    var app = (App)Application.Current;
                    app.CloseSQLiteDatabase();
                    app.FilePath = filePath;
                    var docinfo = app.GetNodeContent("文档信息");

                    // 只保留第一个回车符前的部分
                    if (!string.IsNullOrEmpty(docinfo))
                    {
                        int newlineIndex = docinfo.IndexOf('\n');
                        if (newlineIndex >= 0)
                        {
                            docinfo = docinfo.Substring(0, newlineIndex).Trim();
                            docinfo += "...";
                        }
                    }

                    // 将文件路径推送给 DataGridSysBaseInfo 显示
                    var data = new List<KeyValueInfo>
                            {
                                new KeyValueInfo { Key = "文件路径", Value = filePath },
                                new KeyValueInfo { Key = "文档信息(详情见编辑节点里的'文档信息'节点)", Value = docinfo }
                            };
                    DataGridSysBaseInfo.ItemsSource = data;
                    nodeControl.LoadNodes();
                }
            }
        }

        private void ButtonEditNode_Click(object sender, RoutedEventArgs e)
        {
            // 显示 Edit2Window 窗体
            MyWindowContainer.Content = nodeControl;
            ButtonEditNode.Background = new SolidColorBrush(Colors.LightGreen);
            ButtonEditRel.Background = new SolidColorBrush(Colors.LightGray); // 重置其他按钮颜色
            ButtonSearch.Background = new SolidColorBrush(Colors.LightGray); // 重置其他按钮颜色
        }
        private void ButtonEditRel_Click(object sender, RoutedEventArgs e)
        {
            // 显示 Edit2Window 窗体
            MyWindowContainer.Content = edit2Control;
            ButtonEditRel.Background = new SolidColorBrush(Colors.LightGreen);
            ButtonEditNode.Background = new SolidColorBrush(Colors.LightGray); // 重置其他按钮颜色
            ButtonSearch.Background = new SolidColorBrush(Colors.LightGray); // 重置其他按钮颜色
        }

        private void ButtonSearch_Click(object sender, RoutedEventArgs e)
        {
            // 显示 SearchWindow 窗体
            MyWindowContainer.Content = searchControl;
            ButtonSearch.Background = new SolidColorBrush(Colors.LightGreen);
            ButtonEditNode.Background = new SolidColorBrush(Colors.LightGray); // 重置其他按钮颜色
            ButtonEditRel.Background = new SolidColorBrush(Colors.LightGray); // 重置其他按钮颜色
            searchControl.Width = MyWindowContainer.ActualWidth;
            searchControl.Height = MyWindowContainer.ActualHeight;
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            var data = new List<KeyValueInfo> { };
            DataGridSysBaseInfo.ItemsSource = data;
            var app = (App)Application.Current;
            app.CloseSQLiteDatabase();
        }

        private void ButtonExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //private void MyWindowContainer_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    // 拉伸 SearchWindow 控件
        //    if (MyWindowContainer.Content == searchControl)
        //    {
        //        searchControl.Width = MyWindowContainer.ActualWidth;
        //        searchControl.Height = MyWindowContainer.ActualHeight;
        //    }
        //}
    }

    public class KeyValueInfo
    {
        public required string Key { get; set; }
        public required string Value { get; set; }
    }
}